package com.udaos.modules.enroll.services;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;

import com.udaos.common.base.Service;
import com.udaos.models.enroll.Ud_enroll_product;
import com.udaos.models.region.Ud_region;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.company.services.UdCompanyRegionService;

/**
 * @author mutaotao
 * @time 2017-03-15 11:18:59
 * 
 */
@IocBean(args = { "refer:dao" })
public class UdEnrollProductService extends Service<Ud_enroll_product> {
	
	@Inject
	private UdCompanyRegionService companyRegionService;
	public UdEnrollProductService(Dao dao) {
		super(dao);
	}

	/**
	 * 获取主体下开通的片区
	 * @return
	 */
	public List<Ud_region> getRegions() {
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		return companyRegionService.findRegionById(company_id);
	}

	public NutMap init(int length, int start, int draw, String name) {
		String sqls="SELECT ep.id AS id,c.name AS company_name,g.name AS region_name,ep.name AS name, ep.code AS code,ep.specs AS specs,ep.price AS price,ep.state AS state"
				+" FROM ud_enroll_product ep,ud_company  c,ud_region g "
				+" WHERE ep.company_id=c.id AND ep.region_id=g.id AND ep.delFlag=FALSE";
	//获取主体
	Subject subject = SecurityUtils.getSubject();
	Sys_user user = (Sys_user) subject.getPrincipal();
	String company_id=user.getUnit().getCompany_id();
	sqls+=" AND ep.company_id='"+company_id+"'";
	if(!Strings.isBlank(name)){
		sqls+=" AND ep.name LIKE '%"+name+"%'";
	}
	
	Sql sql =Sqls.create(sqls);
	return this.data(length, start, draw, sql, sql);
	}
}